#coding:utf-8 
import time
import codecs
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os

import json
import hashlib
import datetime
import shutil
import subprocess
from xml.dom import minidom

sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), './utilsPython')))

import glob
import xlrd
import re  
import json  
  
import subprocess
 
outPath='out/' 	#lua输出目录 	 

def run_shell(cmd, cwd=None, quiet=False):
    p = subprocess.Popen(cmd, shell=True, cwd=cwd)
    p.wait() 
    if p.returncode:
        raise Exception('Command %s failed' % cmd)

def stringToInt(_str): 
	_str = str(_str)
	_str = re.sub('\.0*$', "", _str)
	return _str 

def getPngs(customPath,outPath):
    currPath = os.getcwd() 
    outPath = os.path.join(currPath,outPath) 
    array=customPath.split("/")
    choiceFloderName = array[len(array)-1]
    outPath=os.path.join(outPath,choiceFloderName) 
    # print 'customPath:',customPath
    # print 'choiceFloderName:',choiceFloderName 
    # print 'outPath:',outPath
    if( False==os.path.exists(outPath) ):
        os.makedirs( outPath )

    for root , dirs, files in os.walk(customPath):
        for name in files: 
            if name.endswith(".png") or name.endswith(".jpg"):
                imgPath=root+'/'+name 
                print 'imgPath:',imgPath
                run_shell('cp '+imgPath+' '+outPath)
#===================================================================================================
def main():  
    run_shell('rm -r -f '+outPath) 
    if  (os.path.exists(outPath)):
        print 'outPath',outPath
    else:
		os.mkdir( outPath )    

    # print "脚本名：", sys.argv[1]
    getPngs(sys.argv[1],outPath)
    # inputPath = '/Users/pengyuewu/Documents/git/KenGit/client/assets/modules/public/res/picture/changetable/commom'
    # getPngs(inputPath,outPath)
    
if __name__ == '__main__':
	main()